﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Extension
{
    /// <summary>
    /// IEnumerable扩展
    /// </summary>
    public static class IEnumerableExtensions
    {
        /// <summary>
        /// 至少有一项是匹配的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public static bool AtLeastMatchOne<T>(this IEnumerable<T> list, Func<T, bool> predicate)
        {
            if (list == null || !list.Any())
            {
                return false;
            }
            return list.FirstOrDefault(predicate) != null;
        }
        /// <summary>
        /// 转换为分片集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static Slicer<T> ToSlicer<T>(this IEnumerable<T> array, int pageSize)
        {
            return new Slicer<T>(array, pageSize);
        }

        /// <summary>
        /// 判断集合是否为空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arr"></param>
        /// <returns>集合为null或长度为0返回true，否则返回false</returns>
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> arr)
        {
            return arr == null || !arr.Any();
        }
    }
}
